<?php
/**
 * 活动的控制器类
 */ 
class ActivityAction extends ManagerAction{
	public function update(){
		$id=$_REQUEST['id'];
		$Activity = D('Activity');
		if($_POST){
			$_POST['scenic_images'] = json_encode($_POST['images']);
			$_POST['start_time'] = strtotime($_POST['start_time'].':00');
			$_POST['end_time'] = strtotime($_POST['end_time'].':00');
			unset($_POST['images']);
			$Activity->save($_POST);
			$this->success("修改成功");
		}else{
			$activity_info = $Activity->where("id={$id}")->find();
			$activity_info['scenic_images'] = json_decode($activity_info['scenic_images']);
			$this->assign('activity_info',$activity_info);
			$this->display();
		}
	}
	/**
	 * 近期活动列表
	 */
	public function index(){
		$Activity = M('Activity');
		$field = "activity.state,activity.name,user.username,activity.start_time,activity.end_time,activity.min_people,activity.max_people,
		activity.min_age,activity.max_age,activity.id,activity.scenic_images";
		$where = "1";
		$order = "activity.create_time DESC";
		$limit = C('PAGE_COUNT');
		$count = $Activity->where($where)->count();
		$Page = new Page($count, C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$join = "LEFT JOIN user ON user.uid=activity.uid";
		$list = $Activity->where($where)->limit($limit)->join($join)->field($field)->order($order)->select();
		foreach($list as &$v){
			$v['thumb'] = array_shift(json_decode($v['scenic_images']));
		}
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 活动详情
	 */
	public function view(){
		$id = $_GET['id'];
		$Activity = M('Activity');
		$activity_info = $Activity->where("id={$id}")->find();
		$activity_info['thumb'] = array_shift(json_decode($activity_info['scenic_images']));
		$this->assign('activity_info',$activity_info);
		$this->display();
	}
	public function del(){
		$id = intval($_GET['id']);
		/**
		 * 删除缩略图
		 */
		$Activity = M('activity');
		$thumb = $Activity->where("id={$id}")->getField('thumb');
		$thumb = getcwd().'/Public/'.$thumb;

		if(is_file($thumb))	unlink($thumb);
		$Activity->where("id={$id}")->delete();
		$this->success("删除成功");
	}
	/**
	 * 审核活动
	 */
	public function examine(){
		$id = intval($_GET['id']);
		$Activity =  M('activity');
		$state = $Activity->where("id={$id}")->getfield('state');
		$data['id']=$id;
		switch($state){
			case 0://由未验证改为已经验证额
				$data['state']=1;
				break;
			case 1://由已经验证的改为未验证的
				$data['state']=0;
				break;
		}
		$Activity->save($data);
		$this->success("更改成功");
	}
}
?>